﻿using HZU.ZP.Data.Interfaces;
using HZU.ZP.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HZU.ZP.Data.Impl
{
    public class ApplicantDao : IApplicantDao
    {
        private EFCoreDbContext _context;

        public ApplicantDao(EFCoreDbContext context)
        {
            _context = context;
        }
        public void Add(Applicant applicant)
        {
            _context.Add(applicant);
            _context.SaveChanges();
        }

        public Applicant Get(string email)
        {
            return _context.Applicants.FirstOrDefault(e => e.Email == email);
        }

        public Applicant Get(string email, string password)
        {
            return _context.Applicants.FirstOrDefault(e => e.Email == email && e.PassWord == password);
        }
        public List<ApplicantInf> Get(int pageIndex)
        {
            return _context.Applicants.GroupJoin(_context.Informations, e1 => e1.Id, e2 => e2.ApplicantId, (e1, e2) => new
            {
                e1 = e1,
                e2 = e2,
            }).SelectMany(e3 => e3.e2.DefaultIfEmpty(), (e1, e2) => new ApplicantInf
            {
                ApplicantId = e1.e1.Id.Value,
                Name = e2.Name,
                Email = e1.e1.Email,
                Sex = e2.Sex,
                Total = _context.ApplyRecords.Where(e => e.ApplicantId == e1.e1.Id.Value).Count(),
            }).Skip((pageIndex - 1) * 20).Take(20).ToList();
        }

        public int GetCount()
        {
            return _context.Applicants.Count();
        }
    }
}
